package cn.lili.modules.order.order.callback;

import cn.lili.common.vo.ResponseBean;
import cn.lili.common.vo.ResultMessage;
import cn.lili.exchange.AmqpExchangeProperties;
import cn.lili.modules.goods.entity.vos.yunxin.CallBackVO;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.modules.order.aftersale.service.AfterSaleService;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
import cn.lili.modules.order.order.service.OrderItemService;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.modules.order.order.service.RefundFlowService;
import cn.lili.modules.order.trade.entity.enums.AfterSaleStatusEnum;
import cn.lili.routing.AfterSaleRoutingKey;
import cn.lili.util.AmqpMessage;
import cn.lili.util.AmqpSender;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 售后确认退款
 */
@Service("after_sales.complete")
@Slf4j
public class AfterSalesCompleteService implements MessageType{

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    @Lazy
    private OrderService orderService;

    @Autowired
    private RefundFlowService refundFlowService;

    @Autowired
    @Lazy
    private AfterSaleService afterSaleService;

    @Autowired
    private  AmqpSender amqpSender;

    @Autowired
    private  AmqpExchangeProperties amqpExchangeProperties;

    @Override
    public ResponseBean execute(CallBackVO callBackVO) {
        OrderItem orderItem = orderItemService.getOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getSupplyChainOrderId, callBackVO.getOrder_item_id()).last("limit 1"));
        Order orderServiceBySn = orderService.getBySn(orderItem.getOrderSn());

        AfterSale afterSale = afterSaleService.getOne(new LambdaQueryWrapper<AfterSale>().eq(AfterSale::getOrderItemSn, orderItem.getSn()).orderByDesc(AfterSale::getCreateTime).last("limit 1"));
        log.info("afterSale == {}", afterSale);
        if (afterSale == null) {
            Order order = orderService.cancel(orderItem.getOrderSn(), "售后确认退款", orderServiceBySn.getBuyerId());
            order.setSupplyChainState(2);
            orderService.updateById(order);
            return new ResponseBean(0, "售后确认退款", new HashMap<>());
        }
        //退款并把supplyChainState 修改成2（已售后）
        if (orderServiceBySn!= null && (OrderStatusEnum.UNDELIVERED.name().equals(orderServiceBySn.getOrderStatus()) || OrderStatusEnum.OVERVIEW.name().equals(orderServiceBySn.getOrderStatus()))) {
            Order order = orderService.cancel(orderItem.getOrderSn(), "售后确认退款", orderServiceBySn.getBuyerId());
            order.setSupplyChainState(2);
            orderService.updateById(order);
        } else {
            refundFlowService.generatorRefundFlow(afterSale, orderItem, orderServiceBySn);
        }

        //根据售后单号获取售后单
        afterSale.setServiceStatus(AfterSaleStatusEnum.COMPLETE.name());
        afterSaleService.updateById(afterSale);

        //发送退款消息
        amqpSender.send(AmqpMessage.builder().exchange(amqpExchangeProperties.getAfterSale()).routingKey(AfterSaleRoutingKey.AFTER_SALE_STATUS_CHANGE).message(afterSale).build());
        return new ResponseBean(0, "售后确认退款", new HashMap<>());
    }
}
